package com.blindnews.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;

public class NewsContent3 {
    //    搜狐网
    private static int count = -1;

    public static void main(String[] args) {

        // 设置请求地址
        String url = "https://www.163.com/dy/article/ITSDI3OV0514R9P4.html";
        String url2 = "https://mil.sohu.com/a/767150198_121739905?scm=thor.279_13-101000-0_8000.0-0.10095.0_5819";
        String url3 = "https://mil.sohu.com/a/767199948_114911?scm=thor.279_13-101000-0_8000.0-0.10095.0_5819";
        // 设置请求头
        String userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36";
        try {


            // 获取详情页数据
            Document detailDoc = Jsoup.connect(url2).header("User-Agent", userAgent).get();
            System.out.println(detailDoc.text());
            // 获取新闻详情,select获取Post_body的所有子标签；
            Elements newsTitle = detailDoc.select("div.text-title>h1");//获得标题
            System.out.println("Title：" + newsTitle.text());
            Elements newsContent = detailDoc.select("article.article >p");//获得正文

            String[] newsListArr = newsContent.text().split(" ");
            System.out.println("Conten：" + newsListArr.length + "行");

            for (String news : newsListArr) {
                count += 1;

//                将空内容标签过滤掉
                if (count == 0) {
                    continue;//去除中文中的标题
                }
                if (news.isEmpty()) {
                    continue;
                }
//                遇到延伸阅读，不再获取，跳出循环
                if (news.equals("责任编辑：")) {
                    break;
                }
                System.out.println("第" + (count - 1) + "段");
                System.out.println(news);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
